/*
 * Copyright (C) 2012 yueyueniao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hqw.mindanews.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cn.bmob.v3.BmobUser;
import cn.pedant.SweetAlert.SweetAlertDialog;
import cn.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;

import com.hqw.mindanews.LoginActivity;
import com.hqw.mindanews.MainActivity;
import com.hqw.mindanews.MyNewsActivity;
import com.hqw.mindanews.PublishedActivity;
import com.hqw.mindanews.R;
import com.hqw.mindanews.SettingActivity;
import com.hqw.mindanews.bean.User;
import com.hqw.mindanews.fragment.ViewPageFragment.MyAdapter;
import com.hqw.mindanews.utils.Toastor;

public class LeftFragment extends Fragment implements OnClickListener {
	private ImageView imageView;
	private Toastor mToastor;
	private TextView mSendNews;
	private SettingFragment settingFragment;
	private android.support.v4.app.FragmentManager fragmentManager;
	private MainActivity mActivity;
	private ArrayList<Fragment> pagerItemList = new ArrayList<Fragment>();
	private MyAdapter mAdapter;
	private ViewPager mPager;
	private User currentUser;
	private TextView mMyNews;
	private TextView mAdminApply;
	private TextView mUserName;
	private TextView mTips;
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		currentUser = BmobUser.getCurrentUser(getActivity(),User.class);

	}

	private View rootView;// 缓存Fragment view

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (rootView == null) {
			rootView = inflater.inflate(R.layout.left, null);
		}

		init();

		// fragmentManager = getFragmentManager();
		return rootView;
	}

	private void init() {

		imageView = (ImageView) rootView.findViewById(R.id.head_view);
		mTips = (TextView) rootView.findViewById(R.id.grade_textview);
		mUserName = (TextView) rootView.findViewById(R.id.name_textview);
		if (currentUser != null ) {
			if (currentUser.getBmobFile()!=null) {
				//加载头像
				currentUser.getBmobFile().loadImage(getActivity(), imageView);

			}
			String userName = currentUser.getUsername();
			mUserName.setText(userName);
			mTips.setText("欢迎您");
		}
		Resources res = getActivity().getResources();
		mAdminApply = (TextView) getActivity().findViewById(R.id.my_wallet_textview);
		//mAdminApply.setVisibility(View.VISIBLE);
		if (currentUser!=null) {
			if (currentUser.getMobilePhoneNumber()!="13005982409") {
				//mAdminApply.setVisibility(View.VISIBLE);
			}
		}
		
		imageView.setOnClickListener(this);
		mSendNews = (TextView) rootView.findViewById(R.id.my_question_textview);
		mSendNews.setOnClickListener(this);
		mToastor = new Toastor(getActivity());
		mMyNews = (TextView) rootView.findViewById(R.id.my_sendNews_textview);
		mMyNews.setOnClickListener(this);
	}

	@Override
	public void onAttach(Activity activity) {
		mActivity = (MainActivity) activity;
		super.onAttach(activity);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.head_view:
			if (currentUser != null) {
				Intent intent = new Intent(getActivity(), SettingActivity.class);
				startActivity(intent);
			}else {
				new SweetAlertDialog(getActivity(), SweetAlertDialog.ERROR_TYPE)
				.setTitleText("Oops...")
				.setContentText("请先登录吧亲O(∩_∩)O")
				.setConfirmClickListener(new OnSweetClickListener() {

					@Override
					public void onClick(
							SweetAlertDialog sweetAlertDialog) {
						sweetAlertDialog.cancel();
						Intent it = new Intent(getActivity(),
								LoginActivity.class);
						startActivity(it);
						getActivity().finish();
					}
				}).show();
			}
			
			break;
		case R.id.my_question_textview:
			if (currentUser != null) {
				Intent it = new Intent(getActivity(), PublishedActivity.class);
				startActivity(it);
				getActivity().finish();
			} else {
				new SweetAlertDialog(getActivity(), SweetAlertDialog.ERROR_TYPE)
						.setTitleText("Oops...")
						.setContentText("请先登录吧亲O(∩_∩)O")
						.setConfirmClickListener(new OnSweetClickListener() {

							@Override
							public void onClick(
									SweetAlertDialog sweetAlertDialog) {
								sweetAlertDialog.cancel();
								Intent it = new Intent(getActivity(),
										LoginActivity.class);
								startActivity(it);
								getActivity().finish();
							}
						}).show();
			}
			break;
		case R.id.my_sendNews_textview:
			Intent myNewsIntent = new Intent(getActivity(), MyNewsActivity.class);
			startActivity(myNewsIntent);
//			Log.i("bmob", "摧毁");
//			getActivity().finish();
		default:
			break;
		}

	}

	@Override
	public void onResume() {
		if (currentUser != null ) {
			if (currentUser.getBmobFile()!=null) {
				//加载头像
				Log.i("bmob", "重新渲染头像"); 
			    currentUser = BmobUser.getCurrentUser(getActivity(), User.class);
				currentUser.getBmobFile().loadImage(getActivity(), imageView);
				mUserName.setText(currentUser.getUsername());
			}
			
		}
		super.onResume();
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		Log.i("bmob", "被摧毁了"); 
		super.onDestroy();
	}
	private void hideFragments(
			android.support.v4.app.FragmentTransaction transaction) {

	}

}
